package com.example.rabbit.listener;


import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * 消息 接收者
 */
@Component
public class Receiver {
    //平均分摊给多个消费者进行处理，而不是每个消费者都收到所有的消息并处理
    @RabbitListener(queues = "hello_queue")
    public void getHello_queue_Msg(@Payload String mail,@Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(deliveryTag);
        System.out.println("消费者1从hello_queue队列取出消息"+mail.toString());
    }
    //平均分摊给多个消费者进行处理，而不是每个消费者都收到所有的消息并处理
  //  @RabbitListener(queues = "hello_queue")
    public void getHello_queue_Msg2(@Payload String mail) throws IOException {
        System.out.println("消费者2从hello_queue队列取出消息,并抛了异常。但是依然被消费掉了"+mail.toString());
        throw new RuntimeException("消费者2从hello_queue队列取出消息。并抛了异常，但是依然被消费掉了");
    }
}
